<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流水线</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <style>
      .layui-quote-red{border-style:solid;border-color:#FF5722;border-width:1px 1px 1px 5px;background:0 0}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="cicdName" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">应用名</label>
                            <div class="layui-input-inline">
                              <select name="appname" lay-filter="appName" lay-search="" id="appName">
          	              			<option value="" selected="">选择应用</option>
              		            </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>                        
                    </div>
                </form>
            </div>
        </fieldset>
    <div class="layuimini-main">
        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
</body>
<script type="text/html" id="cicdTypeTpl">
  {{# if (d.cicd_type == 1) { }}
     <span style="color:#218868">阿里云流水线</span>
	{{# } else { }}
    <span style="color:#1E9FFF">本地流水线</span>
{{# } }}
</script>
<script type="text/html" id="statusTpl">
  {{# if (d.status == 1) { }}
     <span style="color:#009688">成功</span>
  {{# } else if (d.status == 2) { }}
     <span style="color:#FFB800">运行中</span>
  {{# } else if (d.status == 3) { }}
     <span style="color:#FF5722">失败</span>
	{{# } else { }}
    <span style="color:#C2C2C2">停止</span>
{{# } }}
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addAliFlow"><i class="layui-icon">&#xe61f;</i>添加阿里云流水线</button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="AliFlowSet"><i class="layui-icon">&#xe716;</i>阿里云帐号设置</button>
    </div>
</script>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-sm" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="run">运行</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">删除</a>
</script>
<script>

	  $.get('/cicd/v1/ListAppname', function (resp) {
  		$.each(resp.data,function(i,item){
  			var html = '<option value="'+item.appname+'">'+item.appname+'</option>'	
  			$("#appName").append(html);
  		});
	    layui.use('form', function(){
	           var form = layui.form;
	           form.render();
	    });		
    });	

  //状态提交
  function PostStatus(cicdId,status,lastRunTime) {
    var state = 1;
    if (status == "SUCCESS"){
      state = 1;
    }else if (status == "RUNNING"){
      state = 2;
    }else if (status == "FAIL"){
      state = 3;
    }else{
      state = 1;
    }
       $.ajax({
         url: "/cicd/v1/PostStatus?cicdId=" + cicdId+"&status="+state+"&lastRunTime="+lastRunTime,
         type: "GET",
         dataType: "json",
         success: function (resp) {
                   if(resp.code != 0){
                      //layer.msg('拒绝成功', {icon: 1});                    
		                  //window.location.reload();
                      console.log(resp);
                   }
          }
        });		  	  
  }

    layui.use(['form', 'table','miniTab','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            table = layui.table;
            miniTab = layui.miniTab,
            miniTab.listen();

        table.render({
            elem: '#currentTableId',
            url: '/cicd/v1/List'+location.search,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'cicd_name',title: '流水线名称', sort: true},
                {field: 'appname',title: '应用名', sort: true},
                {field: 'cluster_id',title: '集群', sort: true},
                {field: 'namespace',title: '命名空间', sort: true},
                {field: 'cicd_type',title: '类型', sort: true,templet: '#cicdTypeTpl',},
                {field: 'status', width: 150,title: '状态', sort: true,templet: '#statusTpl'},
                {field: 'remarks', title: '备注', sort: true,hide:true},
                {field: 'last_runtime', width:180, title: '上次运行'},
                {field: 'createtime', width:180, title: '创建时间',hide:true},
                {title: '操作', minWidth:300,toolbar: '#currentTableBar', align: "center"}
            ]],
            //size:'lg',
            limits: [25, 50, 100],
            limit: 25,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            data.field.cicdName = data.field.cicdName.replace(/^\s*|\s*$/g,"");
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/cicd/v1/List',
                //contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){}
            }, 'data');
            return false;
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'addAliFlow') {  // 监听添加操作
                var index = layer.open({
                    title: '创建',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['55%', '80%'],
                    content: '/page/cicd/pipelines_add.html?v=1',
                    end: function(){
                    	window.location.reload();//关闭open打开的页面时，刷新父页面
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if(obj.event === 'addCicd'){
              console.log('no');
            }else if(obj.event === 'AliFlowSet'){
                var index = layer.open({
                    title: '阿里云帐号设置',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['55%', '85%'],
                    content: '/page/cicd/pipelines_setting.html?v=1'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                var index = layer.open({
                    title: data.cicd_name+'流水线',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['97%', '97%'],
                    content: '/page/cicd/index.html?cicdName='+data.cicd_name+'&cicdId='+ data.id
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'edit') {               
                var index = layer.open({
                    title: data.cicd_name+'编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['53%', '70%'],
                    content: '/page/cicd/pipelines_edit.html?cicdName='+data.cicd_name+'&cicdId='+ data.id
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;              
            } else if (obj.event === 'run') {   
                layer.confirm('确定运行'+data.cicd_name+'?', {icon: 3, title:'运行提示',yes: function(index){            
                    var index2 = layer.load(0, {shade: false});
                    $.ajax({
                     url: "/cicd/v1/GetPipelines?cicdId=" +data.id,
                     type: "GET",
                     dataType: "json",
                     success: function (resp) {
                          if(resp.code != 0){
                            layer.msg("获取流水线信息失败",{icon:2});
                            return; 
                          }
                          $.ajax({
                           url: "/cicd/pipeline/Start?aliyun_id="+resp.data.aliyun_id+"&organization_id="+resp.data.organization_id+"&pipeline_id="+resp.data.pipeline_id,
                           type: "GET",
                           dataType: "json",
                           success: function (resp2) {
                                      layer.close(index2);
                                      if(resp2.success == true){
                                        PostStatus(data.id,'RUNNING');
                                        layer.msg('启动成功', {icon: 1,time:2000},function(){
                                            location.reload();
                                        });                          
                                      }else{     
                                        layer.msg(resp2.errorMessage,{icon:2});
                                      }
                            },
                            fail:function(){
                              layer.msg("调用启动失败,请重试!",{icon:2},function(){
                                layer.close(index2);
                              });                
                            }
                          });
                      },
                      fail:function(){
                        layer.msg("获取流水线信息失败,请重试!",{icon:2},function(){
                          layer.close(index2);
                        });                
                      }
                    });
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    }); 
                return false;              
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除'+data.cicd_name+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行1-2s左右');
                			 $.ajax({
                			    url: '/cicd/v1/Del?id='+ data.id,
                			    type: "get",
                			    success: function (resp) {
                            layer.close(index2);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                                obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });
    });
</script>
</html>